<%--
  Created by IntelliJ IDEA.
  User: 75880
  Date: 2024/8/16
  Time: 19:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <%
    String base = request.getContextPath() + "/";
    String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + base;
  %>
  <base href="<%=url%>">
  <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
  <script type="text/javascript">
    $(document).ready(function () {
      $.get("book/selectAll", function (books) {
        queryAll(books);
      });

      $("[name='button']").click(function () {
        var query = $("select").val();
        var str = $("[name='text']").val();
        $.get("book/queryByBook", {"query": query, "str": str}, function (books) {
          $("tbody").empty();
          for (var i = 0; i < books.length; i++) {
            queryAll(books);
          }
        })
      });

      $("[value='新增图书']").click(function () {
        window.location.href="addBook.jsp";
      })

      function queryAll(books) {
        for (var i = 0; i < books.length; i++) {
          $("<tr>" +
                  "<td>" + books[i].bookName + "</td>" +
                  "<td>" + books[i].bookAuthor + "</td>" +
                  "<td>" + books[i].bookPublish + "</td>" +
                  "<td>" + books[i].bookPage + "</td>" +
                  "<td>" + books[i].bookPrice + "</td>" +
                  "<td><input type='button' value='删除' id=" + books[i].bookId + ">" +
                  "<a>" +
                  "<input type='button' value='修改' id=" + books[i].bookId + ">" +
                  "</a></td>" +
                  "</tr>").appendTo("tbody")
        }
      }

      $(document).on("click","[value='删除']",function () {
        var bookId=$(this).attr("id");
        $.get("book/delete",{"bookId":bookId},function (bookMess) {
          if(bookMess.boo) {
            alert("删除成功")
          }else {
            alert("删除失败")
          }
          $("tbody").empty();
            queryAll(bookMess.list)
        })
      });

      $(document).on("click","[value='修改']",function () {
        var id = $(this).attr("id");
        window.location.href="updateBook.jsp?bookId="+id;
      })



    });

  </script>
  <body>

  <select>
    <option value="-1">请选择</option>
    <option value="1">书名</option>
    <option value="2">作者</option>
    <option value="3">出版社</option>
  </select>
  <input type="text" name="text">
  <input type="button" name="button" value="查找">

  <input type="button" value="新增图书">

  <table border="1">
    <thead>
    <tr style="background-color: brown">
      <td>书名</td>
      <td>作者</td>
      <td>出版社</td>
      <td>页数</td>
      <td>价格</td>
      <td style="background-color: blue">操作</td>
    </tr>
    </thead>
    <tbody>

    </tbody>
  </table>
  </body>
</html>
